//
//  ChannelViewController.m
//  RTMKitDemo
//
//  Created by reyzhang on 2021/12/31.
//

#import "ChannelViewController.h"
#import "RTMManager.h"
#import "UIViewController+HUD.h"
#import "UIViewController+Helper.h"
#import "ChatRoomViewController.h"
@interface ChannelViewController () <YMRTMChannelDelegate>
@property (nonatomic, strong)  YMRTMChannel *channel;
@property (nonatomic,strong)UIButton *joinChannleBtn;
@end

@implementation ChannelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupForDismissKeyboard];
    self.joinChannleBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.joinChannleBtn.backgroundColor = [UIColor orangeColor];
    [self.joinChannleBtn addTarget:self action:@selector(joinChannel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.joinChannleBtn];
}

-(void)joinChannel{
    
    self.channel =  [[YMRTMManager client] createChannelById:@"100"];
    self.channel.delegate = self;
    
    //加入频道
    [self.channel joinChannelWithCompletion:^(YMRTMJoinChannelStatusCode code) {
        self.showLabel.text = [NSString stringWithFormat:@"成功加入频道：%@",self.channelTF.text];
    }];

    ChatRoomViewController *vc = [[ChatRoomViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (IBAction)joinAction:(id)sender {
    if (self.channelTF.text.length == 0) {
        return;
    }
    
    self.channel =  [[YMRTMManager client] createChannelById:self.channelTF.text];
    self.channel.delegate = self;
    
    //加入频道
    [self.channel joinChannelWithCompletion:^(YMRTMJoinChannelStatusCode code) {
        self.showLabel.text = [NSString stringWithFormat:@"成功加入频道：%@",self.channelTF.text];
    }];
}

- (IBAction)leaveAction:(id)sender {
    if (!self.channel) {
        return;
    }
    
    YMRTMMessage *message = [YMRTMMessage createLeaveChannelMessageWithChannelId:self.channelTF.text];
    
    [self.channel sendMessage:message completion:^(YMRTMSendChannelMessageStatusCode code) {
        self.showLabel.text = [NSString stringWithFormat:@"已离开频道:%@",self.channelTF.text];
    }];
    
   
}

- (IBAction)sendAction:(id)sender {
    
    if (!self.channel) {
        [self showHint:@"请选加入频道"];
        return;
    }
    
    if (self.messageTF.text.length == 0) {
        [self showHint:@"请输入要发送的消息"];
        return;
    }
    
    YMRTMMessage *message = [YMRTMMessage createChannelMessageWithChannelId:self.channelTF.text data:self.messageTF.text];
    
    [self.channel sendMessage:message completion:^(YMRTMSendChannelMessageStatusCode code) {
        self.showLabel.text = [NSString stringWithFormat:@"频道消息发送成功：%@",self.messageTF.text];
    }];
}

#pragma mark - RTMChannelDelegate
- (void)rtmChannel:(YMRTMChannel *)channel memberLeft:(YMRTMMember *)member {
    self.showLabel.text = [NSString stringWithFormat:@"%@离开了频道",member.userId];
}

- (void)rtmChannel:(YMRTMChannel *)channel memberJoined:(YMRTMMember *)member {
    self.showLabel.text = [NSString stringWithFormat:@"%@加入了频道",member.userId];
}

- (void)rtmChannel:(YMRTMChannel *)channel didReceiveMessage:(YMRTMMessage *)message {
    self.showLabel.text = [NSString stringWithFormat:@"收到来自%@的消息：%@",message.uid,message.data];
}

- (void)rtmChannel:(YMRTMChannel *)channel getMembers:(NSArray<YMRTMMember *> *)members {
    NSLog(@"channel current members:%@",members);
}
@end
